package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.DailyComment;
import cn.wolfcode.trip.base.query.DailyQuery;
import cn.wolfcode.trip.base.service.IDailyCommentService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 日报评论资源控制器
 */
@RestController
@RequestMapping("dailyComments")
public class DailyCommentController {
    @Autowired
    private IDailyCommentService dailyCommentService;

    @PostMapping
    public JSONResult register(DailyComment dailyComment) {
        JSONResult json = new JSONResult();
        dailyComment.setUser(UserContext.getCurrentUser());
        dailyCommentService.save(dailyComment);
        return json;
    }

    @GetMapping
    public PageInfo list(DailyQuery qo){
        //按照创建时间倒序
        qo.setOrderBy("dc.createTime desc , c_createTime desc");
        PageInfo pageInfo = dailyCommentService.query(qo);
        return pageInfo;
    }

    @GetMapping("/{dailyId}")
    public int getById(@PathVariable Long dailyId){
        return dailyCommentService.getCount(dailyId);
    }
    @GetMapping("/index/{dailyId}")
    public List<DailyComment> getDailyCommentById(@PathVariable Long dailyId){
        return dailyCommentService.getDailyCommentById(dailyId);
    }
}
